/**
 * @param {number[]} candidates
 * @param {number} target
 * @return {number[][]}
 */
var combinationSum = function (candidates, target) {
  //定义一个数组：存储所有符合条件组合
  let result = []
  //定义一个数组：实时存储当前路径
  let path = []
  //定义一个变量：实时存储数组path中的元素和，初始化为0
  let pathSum = 0

  //翻转数组candidates
  candidates = candidates.reverse()
  //定义一个回溯函数
  function backTrack(start) {
    //如果变量pathSum等于target，说明找到了一个符合条件的组合
    if (pathSum === target) {
      result.push([...path])
      return
    }
    //如果当前变量pathSum大于target，返回
    if (pathSum >= target) {
      return
    }
    for (let i = start; i < candidates.length; i++) {
      path.push(candidates[i])
      pathSum += candidates[i]
      backTrack(i)
      path.pop()
      pathSum -= candidates[i]
    }
  }
  //调用回溯函数
  backTrack(0)

  //返回数组result
  return result
};
console.log(combinationSum([2, 3, 6, 7], 7))